package com.lnsoft.cac.server.handle.webservice;

import com.lnsoft.cac.server.handle.CACGlobalData;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.common.gzip.GZIPInInterceptor;
import org.apache.cxf.transport.common.gzip.GZIPOutInterceptor;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WSClientFactory {

    @Autowired
    private CACGlobalData cacGlobalData;

    public WSClientFactory() {
    }


    /** @deprecated */
    @Deprecated
    public static void startDynamicClient(String wsdlUrl, String methodName, Long timeout, Object... params) throws Exception {
        JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
        Client client = null;

        try {
            System.out.println("URL: " + wsdlUrl + ", api: " + methodName);
            client = clientFactory.createClient(wsdlUrl);
        } catch (RuntimeException var8) {
            System.out.println("与服务端建立连接时出错!" + var8);
            throw var8;
        }

        if (client != null) {
            HTTPConduit conduit = (HTTPConduit)client.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            policy.setConnectionTimeout(timeout);
            policy.setReceiveTimeout(timeout);
            conduit.setClient(policy);
        }

        client.invoke(methodName, params);
    }

    public static Object[] startDynamicResultClient(String wsdlUrl, String methodName, Long timeout, Object... params) {
        JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
        Client client = null;

        try {
            System.out.println("URL: " + wsdlUrl + ", api: " + methodName);
            client = clientFactory.createClient(wsdlUrl);
        } catch (RuntimeException var9) {
            System.out.println("与服务端建立连接时出错!" + var9);
            return new Object[]{"client error!!"};
        }

        HTTPConduit conduit;
        if (client != null) {
            conduit = (HTTPConduit)client.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            policy.setConnectionTimeout(timeout);
            policy.setReceiveTimeout(timeout);
            conduit.setClient(policy);
        }

        conduit = null;

        Object[] objs;
        try {
            objs = client.invoke(methodName, params);
        } catch (Exception var8) {
            objs = new Object[]{"调用服务端报错：methodName:" + methodName + "。" + var8.getMessage()};
            System.out.println("调用服务端方法出错" + var8);
        }

        return objs;
    }

    public static Object[] startDynamicResultClientWithGZip(String wsdlUrl, String methodName, Long timeout, Object... params) {
        JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
        Client client = null;

        try {
            System.out.println("URL: " + wsdlUrl + ", api: " + methodName);
            client = clientFactory.createClient(wsdlUrl);
            client.getInInterceptors().add(new GZIPInInterceptor());
            client.getOutInterceptors().add(new GZIPOutInterceptor());
        } catch (RuntimeException var9) {
            System.out.println("与服务端建立连接时出错" + var9);
            return new Object[]{"ERROR"};
        }

        HTTPConduit conduit;
        if (client != null) {
            conduit = (HTTPConduit)client.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            policy.setConnectionTimeout(timeout);
            policy.setReceiveTimeout(timeout);
            conduit.setClient(policy);
        }

        conduit = null;

        Object[] objs;
        try {
            objs = client.invoke(methodName, params);
        } catch (Exception var8) {
            objs = new Object[]{"调用服务端报错：methodName:" + methodName + "。" + var8.getMessage()};
            System.out.println("调用服务端方法出错" + var8);
        }

        return objs;
    }

    public static Object[] startResultClientWithGZip(Class<?> clientInterface, String wsdlUrl, String methodName, Long timeout, Object... params) {
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(clientInterface);
        factory.setAddress(wsdlUrl);
        Object dataRcvProxy = factory.create();
        Client client = null;

        try {
            System.out.println("URL: " + wsdlUrl + ", api: " + methodName);
            client = ClientProxy.getClient(dataRcvProxy);
        } catch (RuntimeException var11) {
            System.out.println("与服务端建立连接时出错" + var11);
            return new Object[]{"ERROR"};
        }

        HTTPConduit conduit;
        if (client != null) {
            client.getInInterceptors().add(new GZIPInInterceptor());
            client.getOutInterceptors().add(new GZIPOutInterceptor());
            conduit = (HTTPConduit)client.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            policy.setConnectionTimeout(timeout);
            policy.setReceiveTimeout(timeout);
            conduit.setClient(policy);
        }

        conduit = null;

        Object[] objs;
        try {
            objs = client.invoke(methodName, params);
        } catch (Exception var10) {
            objs = new Object[]{"调用服务端报错：methodName:" + methodName + "。" + var10.getMessage()};
            System.out.println("调用服务端方法出错" + var10);
        }

        return objs;
    }

    public Object[] uploadCACConfig(Object... params) {
        String strMethodName = "uploadCACConfig";
        return WSClientFactory.startDynamicResultClient(cacGlobalData.cagWsUrl, strMethodName, (long)30000, params);
    }

    public Object[] uploadCACData(Object... params) {
        String strMethodName = "uploadCACData";
        return WSClientFactory.startDynamicResultClient(cacGlobalData.cagWsUrl, strMethodName, (long)30000, params);
    }

    public Object[] uploadHeartbeatInfo(Object... params) {
        String strMethodName = "uploadCACHeartbeatInfo";
        return WSClientFactory.startDynamicResultClient(cacGlobalData.cagWsUrl, strMethodName, (long)30000, params);
    }
}
